package com.csx.kotlndemo;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import com.csx.kotlndemo.base.BaseActivity;
import com.csx.kotlndemo.databinding.ActivityMainBinding;
import com.csx.kotlndemo.viewmodel.MainViewModel;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;

public class MainActivity extends BaseActivity<MainViewModel, ActivityMainBinding> {

    Button btnTest;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        viewBinding.include.btnTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewModel.testValue("fjsdojfosdjfdos").observe(MainActivity.this, new Observer<String>() {
                    @Override
                    public void onChanged(String s) {
                        show(s);
                    }
                });
            }
        });

        viewBinding.include.btnTestLive.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
//                viewModel.getData(MainActivity.this);
                getData();
            }
        });


    }

    public void getData(){
        viewModel.getData().observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                refresh(s);
            }
        });
    }

    public void refresh(String s){
        viewBinding.include.tvTestCon.setText("当前的值==>"+s);
    }

    @Override
    public void showError(Object o) {
        viewBinding.include.tvTestCon.setText(o.toString());
    }

    /**
     * 写一个RecycleView，通用Adapter，测试没有前台时，用LiveData来实现数据显示和直接数据显示的差别
     * @param str
     */
    public void show(String str){
        viewBinding.include.tv.setText("show=>"+str);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
